package com.yzy.soft.system.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.common.collect.Lists;
import com.yzy.soft.common.persistence.dao.MetadataMapper;
import com.yzy.soft.common.persistence.model.Metadata;
import com.yzy.soft.core.base.controller.BaseController;
import com.yzy.soft.core.base.tips.Tip;
import com.yzy.soft.system.dto.TableDataDto;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yinzhiyuan
 * @since 2018-02-08
 */
@RestController
@RequestMapping("/metadata")
public class MetadataController extends BaseController {
    @Autowired
    private MetadataMapper metadataMapper;


    @GetMapping("")
    public TableDataDto getAll(@RequestParam(name="page",defaultValue = "1",required = true) int page, @RequestParam(name="limit",defaultValue = "10",required = true) int pageSize) {
        Wrapper<Metadata> metadataattr=new EntityWrapper<Metadata>();
        page=(page-1)*pageSize;
        pageSize=page+pageSize;
        List<Metadata> result=metadataMapper.selectPage(new RowBounds(page,pageSize),metadataattr);
        TableDataDto tableDataDto=new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(metadataMapper.selectCount(metadataattr));
        tableDataDto.setData(result);
        return tableDataDto;
    }
    @GetMapping("/{by}")
    public TableDataDto getby(@RequestParam(name="page",defaultValue = "1",required = true) int page, @RequestParam(name="limit",defaultValue = "10",required = true) int pageSize,@PathVariable(name="by",required = false) String by) {
        Wrapper<Metadata> metadataattr=new EntityWrapper<Metadata>();
        metadataattr.where("id={0}",by);
        page=(page-1)*pageSize;
        pageSize=page+pageSize;
        List<Metadata> result=metadataMapper.selectPage(new RowBounds(page,pageSize),metadataattr);
        TableDataDto tableDataDto=new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(metadataMapper.selectCount(metadataattr));
        tableDataDto.setData(result);
        return tableDataDto;
    }
    @PostMapping("")
    public Tip add(@ModelAttribute Metadata matadata) {
        matadata.setCreate(new Date());
        matadata.setUpdate(new Date());
        Integer result=metadataMapper.insert(matadata);
        return SUCCESS_TIP;
    }

    @DeleteMapping("/{id}")
    public Tip delete(@RequestParam(name="ids[]",required = false) Integer[] expense,@PathVariable(name="id",required = false) Integer id) {
        //Collections.<String>
        if(null!=id){
            Integer result=metadataMapper.deleteById(id);
        }else if(expense.length!=0){
            Integer result=metadataMapper.deleteBatchIds(Lists.newArrayList(expense) );
        }
        return SUCCESS_TIP;
    }
    @DeleteMapping("/")
    public Tip delete1(@RequestParam(name="ids[]",required = false) Integer[] matadata) {
        if(matadata.length!=0){
            Integer result=metadataMapper.deleteBatchIds(Lists.newArrayList(matadata) );
        }
        return SUCCESS_TIP;
    }
    @PutMapping("")
    public Tip status(@ModelAttribute Metadata matadata) {
        matadata.setUpdate(new Date());
        Integer result=metadataMapper.updateById(matadata);
        return SUCCESS_TIP;
    }
}
